package demo1;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class CardGame {
    public String[] suits = {"♥","♣","♦","♠"};

    /**
     *买牌
     *依次赋值
     */
    public List<Card> buyCards() {
        List<Card> cardList = new ArrayList<>();
        for(int i = 0;i < this.suits.length;i++) {
            for(int j = 1;j <= 13;j++) {
                cardList.add(new Card(suits[i],j));
            }
        }
        return cardList;
    }
    /**
     *洗牌
     * 从后往前进行交换
     */
    public void shuffle(List<Card> cardList) {
        Random random = new Random();

        for(int i = cardList.size() - 1;i > 0;i--) {
            int index = random.nextInt(i);//注意给范围
            swap(cardList,i,index);
        }
    }
    private void swap(List<Card> cardList,int i,int j) {
        Card tmp = cardList.get(i);
        cardList.set(i,cardList.get(j));
        cardList.set(j,tmp);
    }
    /**
     * 发牌
     *每人依次拿牌,共拿五张
     */
    public List<List<Card>> getCard(List<Card> cardList) {
        List<Card> hand1 = new ArrayList<>();
        List<Card> hand2 = new ArrayList<>();
        List<Card> hand3 = new ArrayList<>();

        List<List<Card>> hand = new ArrayList<>();

        hand.add(hand1);
        hand.add(hand2);
        hand.add(hand3);

        for(int i = 0;i < hand.size();i++) {
            for(int j = 0;j < 5;j++) {
                Card ret = cardList.remove(0);
                hand.get(i).add(ret);
            }
        }
        return hand;
    }
}
